//定义入口函数
$(function () {
    findAll();
    findTotal();
    $("#add").click(function () {
        $("#addModal").modal('show');
        json = {"name": "", address: "北京"};
        $("#addForm").populateForm(json);
    });
    $("#confirmAdd").click(function () {
        var name = $("#name1").val();
        var address = $("#address").val();
        //var addForm=$("#addForm").serializeJson();
        $.post("../add.do", {"name": name, "address": address}, function (data) {
            if (data.success) {
                alert(data.message);
                $("#addModal").modal('hide');
                findAll();
                findTotal();
            } else {
                alert(data.message);
            }
        }, "json");
    });
    $("#confirmUpdate").click(function () {
        var updateForm = $("#updateForm").serializeJson();
        $.post("../update.do", updateForm, function (data) {
            if (data.success) {
                alert(data.message);
                $("#updateModal").modal('hide');
                findAll();
                findTotal();
            } else {
                alert(data.message);
            }
        }, "json");
    });
    $("#findByID").click(function () {
        var id = $("#exampleInputName2").val();
        $.post("../findById.do", {"id": id}, function (data) {
            $("#table1").html('<tr><th>学号</th><th>姓名</th><th>地址</th><th>操作</th></th>');
            for (var i = 0; i < data.length; i++) {
                var json = JSON.stringify(data[i]);
                var temp = encodeURI(json);
                var tr = '<tr>' +
                    '<td>' + data[i].id + '</td>' +
                    '<td>' + data[i].name + '</td>' +
                    '<td>' + data[i].address + '</td>' +
                    '<td><a class="btn btn-success" onclick="updateStudent(\'' + temp + '\')" href="javascript:void(0)">' +
                    '<span class="glyphicon glyphicon-edit"></span>' +
                    '</a>' +
                    '<a style="margin-left: 5px" class="btn btn-danger" href="javascript:void(0)" onclick="delUser(' + data[i].id + ')">' +
                    '<span class="glyphicon glyphicon-trash"></span>' +
                    '</a></td>' +
                    '</tr>'
                $("#table1").append(tr);
            }
        }, "json")
    });
    $("#findByName").click(function () {
        var name = $("#exampleInputName3").val();
        $.post("../findByName.do", {"name": name}, function (data) {
            $("#table1").html('<tr><th>学号</th><th>姓名</th><th>地址</th><th>操作</th></th>');
            for (var i = 0; i < data.length; i++) {
                var json = JSON.stringify(data[i]);
                var temp = encodeURI(json);
                var tr = '<tr>' +
                    '<td>' + data[i].id + '</td>' +
                    '<td>' + data[i].name + '</td>' +
                    '<td>' + data[i].address + '</td>' +
                    '<td><a class="btn btn-success" onclick="updateStudent(\'' + temp + '\')" href="javascript:void(0)">' +
                    '<span class="glyphicon glyphicon-edit"></span>' +
                    '</a>' +
                    '<a style="margin-left: 5px" class="btn btn-danger" href="javascript:void(0)" onclick="delUser(' + data[i].id + ')">' +
                    '<span class="glyphicon glyphicon-trash"></span>' +
                    '</a></td>' +
                    '</tr>'
                $("#table1").append(tr);
            }
        }, "json")
    });
    $("#findByAddress").click(function () {
        var address = $("#exampleInputName4").val();
        $.post("../findByAddress.do", {"address": address}, function (data) {
            $("#table1").html('<tr><th>学号</th><th>姓名</th><th>地址</th><th>操作</th></th>');
            for (var i = 0; i < data.length; i++) {
                var json = JSON.stringify(data[i]);
                var temp = encodeURI(json);
                var tr = '<tr>' +
                    '<td>' + data[i].id + '</td>' +
                    '<td>' + data[i].name + '</td>' +
                    '<td>' + data[i].address + '</td>' +
                    '<td><a class="btn btn-success" onclick="updateStudent(\'' + temp + '\')" href="javascript:void(0)">' +
                    '<span class="glyphicon glyphicon-edit"></span>' +
                    '</a>' +
                    '<a style="margin-left: 5px" class="btn btn-danger" href="javascript:void(0)" onclick="delUser(' + data[i].id + ')">' +
                    '<span class="glyphicon glyphicon-trash"></span>' +
                    '</a></td>' +
                    '</tr>'
                $("#table1").append(tr);
            }
        }, "json")
    });
});

var currPage = 1;//当前页
var pageSize = 5;//每页显示的条数
var totalPages = 0;//总页数
function findTotal() {
    $.post("../findTotal.do", function (data) {
        $("ul").html("");
        count = data;
        if (count % pageSize == 0) {
            totalPages = count / pageSize;
        } else {
            totalPages = (count - count % pageSize) / pageSize + 1;
        }
        var pre = "";
        if (currPage == 1) {
            pre = '<li class="disabled"><a href="javascript:void(0);" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
        } else {
            pre = '<li onclick="pre()"><a href="javascript:void(0);" aria-label="Previous"><span aria-hidden="true">&laquo;</span></a></li>';
        }
        $("ul").append(pre);
        for (var i = 1; i <= totalPages; i++) {
            var li = "";
            if (i == currPage) {
                li = '<li class="active"><a href="javascript:void(0);">' + i + '</a></li>';
            } else {
                li = '<li onclick="cc(' + i + ')"><a href="javascript:void(0);">' + i + '</a></li>';
            }
            $("ul").append(li);
        }
        var next = "";
        if (currPage == totalPages) {
            next = '<li class="disabled"><a href="javascript:void(0);" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
        } else {
            next = '<li onclick="next()"><a href="javascript:void(0);" aria-label="Next"><span aria-hidden="true">&raquo;</span></a></li>';
        }
        $("ul").append(next);
        var span = '<span style="font-size: 25px;margin-left: 5px">共' + count + '条记录，共' + totalPages + '页</span>';
        $("ul").append(span);
    }, "json")
}

function findAll() {
    $.post("../findAllStudent.do", {"currPage": currPage, "pageSize": pageSize}, function (data) {
        $("#table1").html('<tr><th>学号</th><th>姓名</th><th>地址</th><th>操作</th></th>');
        for (var i = 0; i < data.length; i++) {
            var json = JSON.stringify(data[i]);
            var temp = encodeURI(json);
            var tr = '<tr>' +
                '<td>' + data[i].id + '</td>' +
                '<td>' + data[i].name + '</td>' +
                '<td>' + data[i].address + '</td>' +
                '<td><a class="btn btn-success" onclick="updateStudent(\'' + temp + '\')" href="javascript:void(0)">' +
                '<span class="glyphicon glyphicon-edit"></span>' +
                '</a>' +
                '<a style="margin-left: 5px" class="btn btn-danger" href="javascript:void(0)" onclick="delUser(' + data[i].id + ')">' +
                '<span class="glyphicon glyphicon-trash"></span>' +
                '</a></td>' +
                '</tr>'
            $("#table1").append(tr);
        }
    }, "json")
}

function delUser(id) {
    $.post("../deleteStudent.do", {"id": id}, function (data) {
        if (data.success) {
            alert(data.message);
            findAll();
            findTotal();
        } else {
            alert(data.message);
        }
    }, "json");
}

function updateStudent(temp) {
    $("#updateModal").modal('show');
    var json = decodeURI(temp);
    var obj = JSON.parse(json);
    $("#updateForm").populateForm(obj);
}

function cc(i) {
    currPage = i;
    findAll();
    findTotal();
}

function pre() {
    currPage--;
    findAll();
    findTotal();
}

function next() {
    currPage++;
    findAll();
    findTotal();
}





